import { ethers } from "ethers";
import store from "../store";
import { payAbi } from "@/utils/abi/abi.js";
import { tokenDecimals,tokenContract,usdtContract,usdtDecimals,lpFee } from '@/utils/config.js'
import eth from '@/utils/eth.js'
import { qutorAbi } from "@/utils/abi/qutorAbi.js";
import { showToast } from "vant";
let provider, Contract, signer, ContractWithSinger
try {
  provider = new ethers.providers.Web3Provider(window.ethereum)
  Contract = new ethers.Contract(payAbi.Contract, payAbi.contractAbi, provider);
  signer = provider.getSigner();
  ContractWithSinger = Contract.connect(signer);
} catch (error) {

}
const pageSize = 100
const pageNumAdd = (params) => {
  params.end = params.end + pageSize
  params.start = params.start + pageSize
  return params
}
const payEth = {
  async isTransferStake(contract,from, to) {
    const Contract = new ethers.Contract(contract || payAbi.Contract, ["function isTransferStake(address _from,address _to) public view returns(bool)"], provider);
    let isCanTransfer = false
    try {
      isCanTransfer = await Contract.isTransferStake(from, to)
    } catch (error) {
    } finally {
      return isCanTransfer
    }
  },
  async getPrice(){
    let price = 0
    try {
      let tokenIn = tokenContract 
      let tokenOut = usdtContract
      let fee = lpFee
      let amountIn  = ethers.utils.parseUnits( '1',  tokenDecimals);
      price = await this.quoteExactInputSingle(tokenIn, tokenOut, fee, amountIn, 0, usdtDecimals)
    } catch (error) {
    }finally{
      return price
    }
  },
  async quoteExactInputSingle( tokenIn, tokenOut, fee, amountIn, priceLimit = 0, decimal) {
    const Contract = new ethers.Contract(qutorAbi.Contract, qutorAbi.contractAbi,  provider );
    
    let value = 0;
    try {
      let a = await Contract.callStatic.quoteExactInputSingle(
        tokenIn,
        tokenOut,
        fee,
        amountIn,
        priceLimit
      );
      value = ethers.utils.formatUnits(a, decimal);
      return value;
    } catch (error) {
      return value;
    }
  },
  async getUserInfo(accounts, contract, decimals) {
    let totalamount = 0
    let totalStakeAmount = 0
    let releaseAmount = 0
    let widthdrawnAmount = 0
    let shopmoney = 0
    let refReward = 0
    try {
      const Contract = new ethers.Contract(contract || payAbi.Contract, payAbi.contractAbi, provider);
      const signer = provider.getSigner();
      const ContractWithSinger = Contract.connect(signer);
      let account = accounts || await eth.getAccount()
      let userInfo = await ContractWithSinger.userInfo(account)
      //  消费数量 算力 待领取收益 邀请奖励 累计收益(已领取的收益) 我的收款
      // 我的消费
      totalamount = parseFloat(Number(ethers.utils.formatUnits(userInfo[0], decimals || tokenDecimals)).toFixed(6))
      store.commit('changeTotalamount', totalamount)
      // 我的算力
      totalStakeAmount = parseFloat(Number(ethers.utils.formatUnits(userInfo[1], decimals || tokenDecimals)).toFixed(0))
      store.commit('changeTotalStakeAmount', totalStakeAmount)
      // 待领取收益
      releaseAmount = parseFloat(Number(ethers.utils.formatUnits(userInfo[2], decimals || tokenDecimals)).toFixed(6))
      store.commit('changeReleaseAmount', releaseAmount)
      console.log(releaseAmount)
      // 邀请奖励
      refReward = parseFloat(Number(ethers.utils.formatUnits(userInfo[3], decimals || tokenDecimals)).toFixed(6))
      store.commit('changeRefReward', refReward)
      // 累计收益
      widthdrawnAmount = parseFloat(Number(ethers.utils.formatUnits(userInfo[4], decimals || tokenDecimals)).toFixed(6))
      store.commit('changeWidthdrawnAmount', widthdrawnAmount)
      // 我的收款
      shopmoney = Number(ethers.utils.formatUnits(userInfo[5], decimals || tokenDecimals)).toFixed(0)
      store.commit('changeShopmoney', shopmoney)
    } catch (err) {
      store.commit('changeTotalamount', totalamount)
      store.commit('changeTotalStakeAmount', totalStakeAmount)
      store.commit('changeReleaseAmount', releaseAmount)
      store.commit('changeRefReward', refReward)
      store.commit('changeWidthdrawnAmount', widthdrawnAmount)
      store.commit('changeShopmoney', shopmoney)
      showToast('获取我的算力失败，请检查网络')
    }
  },
  async getData(contract, decimals, obj) {
    let totalamount = 0
    let totalStakeAmount = 0
    try {
      const Contract = new ethers.Contract(contract, payAbi.contractAbi, provider);
      const signer = provider.getSigner();
      const ContractWithSinger = Contract.connect(signer);
      let account = await eth.getAccount()
      let userInfo = await ContractWithSinger.userInfo(account)
      totalamount = Number(ethers.utils.formatUnits(userInfo[0], decimals || tokenDecimals)).toFixed(0)
      totalStakeAmount = parseFloat(Number(ethers.utils.formatUnits(userInfo[1], decimals || tokenDecimals)).toFixed(6))
      if (!Number(totalamount) && !Number(totalStakeAmount)) {
        obj.isHide = true
      } else {
        obj.isHide = false
      }
      return obj
    } catch (err) {
      obj.isHide = true
      return obj
    }
  },
  async getUserInfoAll(data) {
    try {
      if (data && data.length >= 3) {
        await this.getData(data[2].miningContract, data[2].decimals, data[2])
      }
    } catch (error) {
      
    }
    store.commit('changeMiningPayType', data)
  },
  async getContractInfo(contract, decimals) {
    // 矿池余额 总算力 累计交易总额 当前日收益比例
    let totalPay = 0
    let miningBalance = 0
    let totalPoint = 0
    let calDayReleaseRate = 0
    try {
      const Contract = new ethers.Contract(contract || payAbi.Contract, payAbi.contractAbi, provider);
      const signer = provider.getSigner();
      const ContractWithSinger = Contract.connect(signer);
      let contractInfo = await ContractWithSinger.contractInfo()
      // 矿池余额 总算力 累计交易总额 当前日收益比例
      // 矿池余额
      miningBalance = Number(ethers.utils.formatUnits(contractInfo[0], decimals || tokenDecimals)).toFixed(0)
      store.commit('changeMiningBalance', miningBalance)
      // 总算力
      totalPoint = Number(ethers.utils.formatUnits(contractInfo[1], decimals || tokenDecimals)).toFixed(0)
      store.commit('changeTotalPoint', totalPoint)
      // 累计交易总额
      totalPay = Number(ethers.utils.formatUnits(contractInfo[2], decimals || tokenDecimals)).toFixed(0)
      store.commit('changeTotalPay', totalPay)
      // 当前日收益比例
      calDayReleaseRate = Number(ethers.utils.formatUnits(contractInfo[3], 3))
      store.commit('changeCalDayReleaseRate', calDayReleaseRate)
    } catch (err) {
      showToast('获取算力失败')
      // 先不重置
      // store.commit('changeMiningBalance', miningBalance)
      // store.commit('changeTotalPoint', totalPoint)
      // store.commit('changeTotalPay', totalPay)
      // store.commit('changeCalDayReleaseRate', calDayReleaseRate)
    }
  },
  async getLevel(account, noSet) {
    let payLevel = 0
    try {
      const Contract = new ethers.Contract(contract || payAbi.Contract, payAbi.contractAbi, provider);
      const signer = provider.getSigner();
      const ContractWithSinger = Contract.connect(signer);
      let userInfo = await ContractWithSinger.userInfo(account)
      payLevel = Number(ethers.utils.formatUnits(userInfo[1], tokenDecimals)).toFixed(0)
      !noSet && store.commit('changeMyPayLevel', payLevel)
      return payLevel
    } catch (err) {

    }
  },

}
export default payEth